package cc.spider.web.processor;


import cc.spider.core.IDataProcessor;
import cc.spider.model.FlowData;
import cc.spider.model.ResultInfo;
import cc.spider.web.model.http.Request;
import cc.spider.web.parser.JsoupElement;
import cc.spider.web.parser.JsoupElements;
import cc.spider.web.parser.JsoupParser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BaiduSearchPageProcess implements IDataProcessor {
    @Override
    public void process(FlowData flowData) {
        ResultInfo<String> resultInfo = flowData.getResultInfo();
        Request _r = (Request) flowData.getTaskInfo();
        String content = resultInfo.getResult();
        JsoupParser parser = new JsoupParser(content, _r.getUrl());
        if (_r.getUrl().contains("www.baidu.com/s?")) {
            JsoupElements links = parser.queryAll(".t>a");
            List<String> titles = new ArrayList<>();
            List<String> hrefs = new ArrayList<>();
            for (JsoupElement element : links) {
                String title = element.text();
                String href = element.absUrl("href");
                Request request = new Request();
                request.setUrl(href);
                flowData.addNextTaskInfo(request);
                titles.add(title);
                hrefs.add(href);
            }
            Map<String, Object> map = new HashMap<>();
            map.put("titles", titles);
            map.put("hrefs", hrefs);
            flowData.setFields(map);
        } else if (_r.getUrl().contains("www.baidu.com")) {
            String title = parser.query("title").text();
            String link = _r.getUrl();
            Map<String, Object> map = new HashMap<>();
            map.put("title", title);
            map.put("link", link);
            flowData.setFields(map);
        } else {
            System.err.println("异常");
        }
    }
}
